var xmlGames = new Array();
var playerNameInXML = true;

function closeMenu() {
	$("#newGameMenu").dialog("close");
}

function refreshRuningGames(data) {
	if (data.redirect) {
		// data.redirect contains the string URL to redirect to
		window.location.href = data.redirect;
	}

	$('#selectGames').hide();
	$('#selectGames').empty();

	$.each(data, function(key, val) {
		$.each(val, function(internal_key, internal_val) {
			console.log("Internal Key: " + internal_key + " Internal value: "
					+ internal_val);
			if (internal_key == "name") {
				$('#selectGames').append(
						'<li class="ui-widget-content">' + internal_val
								+ '</li>');
			}
		});
		if (val["isFromXML"] == true) {
			var playerNames = new Array();
			$.each(val["humanPlayersNames"], function(key, playerName) {
				console.log("key: " + key + " player name: " + playerName);
				playerNames.push(playerName);
			});
			var game = {};
			game.name = val["name"];
			game.players = playerNames;
			xmlGames.push(game);
		}

	});
	$('#selectGames').show();
}

$(function() {
	$.getJSON("newGame", {}, function(data) {
		refreshRuningGames(data);
	});
});


function newGameSubmit() {
	var mydata = {
		"name" : $(newGameName).val(),
		"human" : $(newGameHumanPlayers).val(),
		"auto" : $(newGameAutoPlayers).val()
	};

	$.getJSON("newGame", mydata, function(data) {
		refreshRuningGames(data);
		$("#newGameMenu").dialog("close");
	});
}

$(function() {
	$("#selectGames").selectable();
});

$(function() {
	$("#join_selected_game").click(function(event) {
		var selected_game = $(".ui-selected");
		if (selected_game == null || selected_game.size() == 0) {
			alert("Please select an active game");
		} else {
			console.log("You have selected to join " + selected_game.text());
			for(var i=0;i<xmlGames.length;i++){
				if (xmlGames[i].name == selected_game.text()) {
					playerNameInXML = false;
					for(var j=0;j<xmlGames[i].players.length;j++) {
						if (xmlGames[i].players[j] == $("#user_name").val())
							playerNameInXML = true;
					}
				}
			}
		

			if (playerNameInXML == false) {
				alert("Your User Name isn't registered to this Game. Please create a new game, or login with a different User Name");
			} else {
				var mydata = {
				"command" : "C2S_JOIN",
				"game-name" : selected_game.text()
				};

				$.getJSON("runningGame", mydata, function(data) {
					console.log("Got response" + data);
					if (data != null) {
						if (data.redirect) {
							// data.redirect contains the string URL to redirect to
							window.location.href = data.redirect;
						}

						$.each(data, function(key, val) {$.each(val, function(interna_key,internal_val) {
						console.log("Internal Key: " + interna_key + " Internal value: " + internal_val);
						if (interna_key == "name") {
							$('#games').append('<li class="ui-widget-content">' + internal_val + '</li>');
						}
						});
						});
						$().redirect(data);
					}
				});
			}
		}
	});
});

$("#newGameMenu").dialog({
	autoOpen : false,
	height : 300,
	width : 350,
	modal : true,
	buttons : {
		Cancel : function() {
			$(this).dialog("close");
		},
		close : function() {
			allFields.val("").removeClass("ui-state-error");
		}
	}
});

function openGameFromXMLDialog() {
	$("#gameFromXMLMenu").dialog().open();
}

$("#gameFromXMLMenu").dialog({
	autoOpen : false,
	height : 300,
	width : 350,
	modal : true,
	buttons : {
		Cancel : function() {
			$(this).dialog("close");
		},
		close : function() {
			allFields.val("").removeClass("ui-state-error");
		}
	}
});

function openNeGameDialog() {
	$("#newGameMenu").dialog().open();
}

$("#waitingOnPlayers").dialog({
	autoOpen : true,
	height : 300,
	width : 350,
	modal : true
});

$('#upload-form').ajaxForm({
	success : function(msg) {
		$("#gameFromXMLMenu").dialog("close");
		refreshRuningGames(msg);
	},
	error : function(msg) {
		$("#upload-error").text("Couldn't upload file");
	},
	dataType : "json"
});

/*
 * function closeXMLuploadDialog() { $("#gameFromXMLMenu").dialog("close"); }
 */